package com.card.manager.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.card.framework.page.model.PageModel;
import com.card.framework.util.CommonUtil;
import com.card.manager.domain.Msa;
import com.card.manager.services.MsaService;


@Controller
@RequestMapping("/msa")
public class MsaController {
	final static Logger loger = Logger.getLogger(MsaController.class);
	@Autowired(required=true) 
	private MsaService msaServices;
	
	@RequestMapping("/index")
	public String msa(HttpServletRequest request,HttpSession session,
			HttpServletResponse response){
		return "msaList";
	}
	
	
	@RequestMapping("/msaAddWindow")
	public String msaAddWindow(HttpServletRequest request,HttpSession session,
			HttpServletResponse response) {
		return "msaPopWindow";
	}
	@RequestMapping("/msaUpdateWindow")
	public String msaUpdateWindow(HttpServletRequest request,HttpSession session,
			HttpServletResponse response) {
		return "msaPopWindow";
	}
	
	@RequestMapping(value="/queryList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryList(Msa msa,PageModel pageModel){
		return msaServices.selectMsaForPage(msa,pageModel);
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> delete(@RequestParam("json") String json){
		Map<String, String> map = new HashMap<String, String>();
		try {
			msaServices.deleteMsas(CommonUtil.getBean(json, Msa.class));
			map.put("mes", "删除成功");
		} catch (Throwable e) {
			CommonUtil.throwsException(map, e);
			loger.error("delete", e);
		}
		return map;//重定向
	}
	
	@RequestMapping(value="/msaAdd",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> msaAdd(Msa msa){
		System.out.println(msa.getOper());
		Map<String, String> map = new HashMap<String, String>();
		
		try {
			msaServices.addOrUpdate(msa);
			map.put("mes", "操作成功");
		}
		catch (Throwable e) {
			CommonUtil.throwsException(map, e);
			loger.error("addOrUpdate", e);
		}
		return map; 
	}
	@RequestMapping(value="/msaUpdate",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> msaUpdate(Msa msa){
		System.out.println(msa.getOper());
		Map<String, String> map = new HashMap<String, String>();
		
		try {
			msaServices.addOrUpdate(msa);
			map.put("mes", "操作成功");
		}
		catch (Throwable e) {
			CommonUtil.throwsException(map, e);
			loger.error("addOrUpdate", e);
		}
		return map; 
	}
}